using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using CyberSinh.Ogg;

namespace LeisureCenter.MetadataHandlers
{
    class OggMetadataHandler : AudioMetadataHandler
    {
        public override void ReadMetadata(string fileName, Stream stream, bool computeDataHash)
        {
            stream.Position = 0;
            OggTagger oggTag = new OggTagger(stream);

            artist = oggTag.Artist;
            album = oggTag.Album;
            try
            {
                track = int.Parse(oggTag.TrackNumber);
            }
            catch
            {
                track = null;
            }
            title = oggTag.Title;
            try
            {
                year = DateTime.Parse(oggTag.Date).Year;
            }
            catch
            {
                year = null;
            }
            genre = oggTag.Genre;

            if (computeDataHash)
            {
                stream.Position = oggTag.DataOffset;
                this.dataHash = HashStreamFromPosition(stream);
            }
        }
    }

    class OggMetadataHandlerFactory : MetadataHandlerFactory
    {
        public override bool AcceptMedia(string filename, Stream stream)
        {
            if (stream.Length > 0)
            {
                BinaryReader br = new BinaryReader(stream);
                byte[] buffer = br.ReadBytes(4);
                return (buffer[0] == 'O' && buffer[1] == 'g' && buffer[2] == 'g' && buffer[3] == 'S');
            }
            return false;
        }

        public override MetadataHandler CreateMetadata()
        {
            return new OggMetadataHandler();
        }
    }
}
